package com.leostadyn.stackfarm.timestatistics.view.activity.admin.staff;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.adapter.SelectListAdapter;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.model.bean.JobBean;
import com.leostadyn.stackfarm.timestatistics.model.bean.PostBean;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class JobListActivity extends BaseActivity implements View.OnClickListener {

    private TextView nav_title;
    private RecyclerView rv_select_job;
    private SelectListAdapter adapter;
    private List<PostBean> listdata = new ArrayList<>();
    private List<JobBean> jobBeanList = new ArrayList<>();
    private ArrayList<String> mPostIds = new ArrayList<>();
    private ArrayList<String> mPostNames = new ArrayList<>();
    private String mIndex = "-1";
    private String info;
    private ImageView back;
    private TextView add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_list);
        mIndex = getIntent().getStringExtra("mIndex");
        initData();
        initView();
    }

    //初始化数据
    public void initData() {

        AdminApi.getInstance().getJob().subscribe(new OriginalSubscriber<List<JobBean>>(compositeDisposable) {

            @Override
            public void onError(ResponseThrowable e) {
                ToastUtil.show(JobListActivity.this, e.getMsg());
            }

            @Override
            public void onNext(List<JobBean> jobBeans, String msg) {
                jobBeanList = jobBeans;
                if (jobBeanList != null && jobBeanList.size() > 0) {
                    for (JobBean job : jobBeanList) {
                        listdata.add(new PostBean(job.getId(), job.getName(), false));
                    }
                    mPostIds.add("-1");
                }
                adapter = new SelectListAdapter(JobListActivity.this, listdata, mPostIds);
                adapter.setmIndex(mIndex);
                rv_select_job.setAdapter(adapter);
            }
        });

    }
    public void initView()
    {

        nav_title=findViewById(R.id.nav_txt_title);
        nav_title.setText("选择岗位");
        findViewById(R.id.nav_img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JobListActivity.this.finish();
            }
        });
        add = findViewById(R.id.nav_btn_add);
        add.setText("确定");
        add.setVisibility(View.VISIBLE);
        add.setOnClickListener(this);
        rv_select_job=findViewById(R.id.rv_job);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_select_job.setLayoutManager(linearLayoutManager);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_btn_add:
                if (adapter.getPostidLists() != null && adapter.getPostidLists().size() > 0) {
                    Intent intent = new Intent();
                    intent.putExtra("mIndex", adapter.getPostidLists().get(0));
                    intent.putExtra("info", adapter.getNameLists().get(0));

                    setResult(Activity.RESULT_OK, intent);
                    finish();
                    JobListActivity.this.finish();
                } else {
                    ToastUtil.show(this, "请选择岗位");
                }

        }

    }
}
